const express = require("express");
const router = express.Router();

// 导入控制器
const HouseController = require("../controller/HouseController");
const houseController = new HouseController(); // 实例化

// 引入权限检查中间件
const checkRoleMiddleware = require("../middleware/checkRoleMiddleware");

// 定义仓库模块不同操作所需角色列表
const HOUSE_ROLES = {
  CREATE: [1, 2], // 创建仓库和区域: 超管, 库管
  READ: [1, 2, 3], // 查看仓库和区域: 超管, 库管, 采购
  UPDATE: [1, 2], // 修改仓库和区域: 超管, 库管
  DELETE: [1, 2], // 删除仓库和区域限: 超管, 库管
};

// 仓库管理路由

// 获取仓库列表
router.get("/", (req, res) => houseController.getHouseList(req, res));

// 新增仓库
router.post("/", checkRoleMiddleware(HOUSE_ROLES.CREATE), (req, res) =>
  houseController.createHouse(req, res)
);

// 获取仓库详情
router.get("/:id", checkRoleMiddleware(HOUSE_ROLES.READ), (req, res) =>
  houseController.getHouseDetail(req, res)
);

// 更新仓库
router.put("/:id", checkRoleMiddleware(HOUSE_ROLES.UPDATE), (req, res) =>
  houseController.updateHouse(req, res)
);

// 软删除/停用仓库
router.delete("/:id", checkRoleMiddleware(HOUSE_ROLES.DELETE), (req, res) =>
  houseController.deactivateHouse(req, res)
);

// 仓库区域路由 - 子表

// 新增仓库区域
router.post(
  "/:houseId/areas",
  checkRoleMiddleware(HOUSE_ROLES.CREATE),
  (req, res) => houseController.createHouseArea(req, res)
);

// 获取某个仓库下的区域列表
router.get(
  "/:houseId/areas",
  checkRoleMiddleware(HOUSE_ROLES.READ),
  (req, res) => houseController.getHouseAreas(req, res)
);

// 获取区域详情
router.get("/areas/:id", checkRoleMiddleware(HOUSE_ROLES.READ), (req, res) =>
  houseController.getAreaDetail(req, res)
);

// 更新仓库区域
router.put("/areas/:id", checkRoleMiddleware(HOUSE_ROLES.UPDATE), (req, res) =>
  houseController.updateHouseArea(req, res)
);

// 软删除/停用仓库区域
router.delete(
  "/areas/:id",
  checkRoleMiddleware(HOUSE_ROLES.DELETE),
  (req, res) => houseController.deactivateHouseArea(req, res)
);

module.exports = router;
